/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_orm.htm
 *
 * Zhiqim ORM is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.orm.dbo;

import org.zhiqim.kernel.model.maps.MapSS;


/**
 * 批量处理封装
 *
 * @version v1.0.0 @author zouzhigang 2019-3-29 新建与整理
 */
public class Batcher
{
    private BatchType type;
    
    //插入
    private Object insertData;
    private MapSS insertReplaceMap;
    
    //替换
    private Object replaceData;
    private MapSS replaceReplaceMap;
    
    //插入或更新
    private Object insertOrUpdateData;
    private Updater insertOrUpdateUpdater;
    
    //更新
    private Object updateData;
    private MapSS updateReplaceMap;
    
    //更新器
    private Class<?> updaterClass;
    private Updater updaterUpdater;
    
    //删除
    private Class<?> deleteClass;
    private MapSS deleteReplaceMap;
    private Object[] deleteIds;
    private Selector deleteSelector;
    
    /*************************************************************************************/
    //设置
    /*************************************************************************************/
    
    /**
     * 增加数据,传入标准[表类]对象
     * 
     * @param data          表对象
     * @return              当前对象
     */
    public Batcher insert(Object data)
    {
        this.insertData = data;
        
        this.type = BatchType.INSERT;
        return this;
    }

    /**
     * 增加数据，支持表或字段中中有多个替换字段，如LOG_TRACE_$MONTH$_$ID$
     * 
     * @param data          表对象
     * @param replaceMap    适配表
     * @return              当前对象
     */
    public Batcher insert(Object data, MapSS replaceMap)
    {
        this.insertData = data;
        this.insertReplaceMap = replaceMap;
        
        this.type = BatchType.INSERT;
        return this;
    }
    
    /**
     * 替换数据,传入标准[表类]对象和更新器（取fieldMap和replaceMap）当存在时指定修改的值
     * 
     * @param data          表对象
     * @param updater       更新器
     * @return              当前对象
     */
    public Batcher insertOrUpdate(Object data, Updater updater)
    {
        this.insertOrUpdateData = data;
        this.insertOrUpdateUpdater = updater;
        
        this.type = BatchType.INSERT_OR_UPDATE;
        return this;
    }

    /**
     * 替换数据,传入标准[表类]对象
     * 
     * @param data          表对象
     * @return              当前对象
     */
    public Batcher replace(Object data)
    {
        this.replaceData = data;
        
        this.type = BatchType.REPLACE;
        return this;
    }

    /**
     * 替换数据，支持表或字段中有多个替换字段，如LOG_TRACE_$MONTH$_$ID$
     * 
     * @param data          表对象
     * @param replaceMap    适配表
     * @return              当前对象
     */
    public Batcher replace(Object data, MapSS replaceMap)
    {
        this.replaceData = data;
        this.replaceReplaceMap = replaceMap;
        
        this.type = BatchType.REPLACE;
        return this;
    }

    /**
     * 更新数据，指定更新器需要更新的字段、条件和可替换表
     * 
     * @param cls           表类
     * @param updater       更新器
     * @return              当前对象
     */
    public Batcher update(Class<?> cls, Updater updater)
    {
        this.updaterClass = cls;
        this.updaterUpdater = updater;
        
        this.type = BatchType.UPDATER;
        return this;
    }
    
    /**
     * 更新数据，指定对象，根据主键进行更新，主键值不更新
     * 
     * @param data          表对象
     * @return              当前对象
     */
    public Batcher update(Object data)
    {
        this.updateData = data;
        
        this.type = BatchType.UPDATE;
        return this;
    }
    
    /**
     * 更新数据，指定对象，根据主键进行更新，主键值不更新，支持可替换表
     * 
     * @param data          表对象
     * @param replaceMap    适配表
     * @return              当前对象
     */
    public Batcher update(Object data, MapSS replaceMap)
    {
        this.updateData = data;
        this.updateReplaceMap = replaceMap;
        return this;
    }
    
    /**
     * 删除数据，多个主键时使用
     * 
     * @param cls           表类
     * @param ids           关键属性为数组，多个主键
     * @return              当前对象
     */
    public Batcher delete(Class<?> cls, Object... ids)
    {
        this.deleteClass = cls;
        this.deleteIds = ids;
        
        this.type = BatchType.DELETE_ID;
        return this;
    }
    
    /**
     * 删除数据，多个主键时使用
     * 
     * @param cls           表类
     * @param replaceMap    替换表
     * @param ids           关键属性为数组，多个主键
     * @return              当前对象
     */
    public Batcher delete(Class<?> cls, MapSS replaceMap, Object... ids)
    {
        this.deleteClass = cls;
        this.deleteReplaceMap = replaceMap;
        this.deleteIds = ids;
        
        this.type = BatchType.DELETE_ID;
        return this;
    }
    
    /**
     * 删除数据, 根据条件
     * 
     * @param cls           表类
     * @param selector      对象选择器
     * @return              当前对象
     */
    public Batcher delete(Class<?> cls, Selector selector)
    {
        this.deleteClass = cls;
        this.deleteSelector = selector == null?new Selector():selector;
        
        this.type = BatchType.DELETE_SELECTOR;
        return this;
    }
    
    /********************************************************************************************/
    //获取
    /********************************************************************************************/
    
    public BatchType getType()
    {
        return type;
    }
    
    public Object getInsertData()
    {
        return insertData;
    }
    
    public MapSS getInsertReplaceMap()
    {
        return insertReplaceMap;
    }
    
    public Object getReplaceData()
    {
        return replaceData;
    }
    
    public MapSS getReplaceReplaceMap()
    {
        return replaceReplaceMap;
    }
    
    public Object getInsertOrUpdateData()
    {
        return insertOrUpdateData;
    }
    
    public Updater getInsertOrUpdateUpdater()
    {
        return insertOrUpdateUpdater;
    }
    
    public Object getUpdateData()
    {
        return updateData;
    }
    
    public MapSS getUpdateReplaceMap()
    {
        return updateReplaceMap;
    }
    
    public Class<?> getUpdaterClass()
    {
        return updaterClass;
    }
    
    public Updater getUpdaterUpdater()
    {
        return updaterUpdater;
    }
    
    public Class<?> getDeleteClass()
    {
        return deleteClass;
    }
    
    public MapSS getDeleteReplaceMap()
    {
        return deleteReplaceMap;
    }
    
    public Object[] getDeleteIds()
    {
        return deleteIds;
    }
    
    public Selector getDeleteSelector()
    {
        return deleteSelector;
    }
}
